package controller;

import editor.MyDateEditor;
import entity.Person;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.PropertiesEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by cmy on 2019/2/20.
 * SpringMVC:@Controller @RequestMapping @ResponseBody @RestController:会为所有的控制器函数设置响应体效果
 *           @RequestParam  @PathVariables
 */
@Controller
@RequestMapping("/main")
public class MainController {
    @InitBinder
    public void dateformate(WebDataBinder wdb){
        wdb.registerCustomEditor(Date.class,new MyDateEditor());
    }
    @RequestMapping("/toMain")
    public String toMain(){
        return "redirect:/main/toMain2";
    }

    @RequestMapping("/toMain2")
    public String toMain2(){
        return "main";
    }

    @RequestMapping("/ajax")
    @ResponseBody
    public Object getPerson(){
        Person person=new Person("张三",18);
        return person;
    }

    @RequestMapping("/getPerson")
    //返回值类型为void 渲染请求的视图
    public void getPerson(Person person){
        System.out.println(person);
    }

    //400:参数传递异常
    @RequestMapping("/getDogName")
    public void getDog(@RequestParam(value = "dogName",required = false,defaultValue ="小黑") String name){
        System.out.println("狗的名字为:"+name);
    }

    @RequestMapping("/getDogName/{dogName}")
    public void getDogName(@PathVariable("dogName")String dogName){
        System.out.println("狗的名字为:"+dogName);
    }

    //Date参数
    @RequestMapping("/getDate")
    public void getDate(Date time){
        System.out.println("传递的日期为:"+time);
    }
}
